<!Doctype html>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0" />
	<title>Python3 函数 | 菜鸟教程</title>

  <meta name='robots' content='max-image-preview:large' />
<link rel='stylesheet' id='classic-theme-styles-css' href='../0/classic-themes.min.css?ver=6.2' type='text/css' media='all' />
<link rel="canonical" href="../python3/python3-function.html" />
<meta name="keywords" content="Python3 函数">
<meta name="description" content="Python3 函数 函数是组织好的，可重复使用的，用来实现单一，或相关联功能的代码段。函数能提高应用的模块性，和代码的重复利用率。你已经知道Python提供了许多内建函数，比如print()。但你也可以自己创建函数，这被叫做用户自定义函数。  定义一个函数  你可以定义一个由自己想要功能的函数，以下是简单的规则：  函数代码块以 def 关键词开头，后接函数标识符名称和圆括号 ()。  任何传入参数和自变量必须放在圆括号中间，圆括号之..">
		
	<link rel="shortcut icon" href="https://static.runoob.com/images/favicon.ico">
	<link rel="stylesheet" href="../0/style.css?v=1.170" type="text/css" media="all" />	
	<link rel="stylesheet" href="../0/font-awesome.min.css" media="all" />	
  <!--[if gte IE 9]><!-->
  <script src=""></script>
  <!--<![endif]-->
  <!--[if lt IE 9]>
     <script src=""></script>
     <script src=""></script>
  <![endif]-->
  <link rel="apple-touch-icon" href="https://static.runoob.com/images/icon/mobile-icon.png"/>
  <meta name="apple-mobile-web-app-title" content="菜鸟教程">
</head>
<body>

<!--  头部 -->
<div class="container logo-search">

  <div class="col search row-search-mobile">
    <form action="index.php">
      <input class="placeholder" placeholder="搜索……" name="s" autocomplete="off">
      
    </form>
  </div>

  <div class="row">
    <div class="col logo">
      <h1><a href="../">菜鸟教程 -- 学的不仅是技术，更是梦想！</a></h1>
    </div>
        <div class="col right-list"> 
    <button class="btn btn-responsive-nav btn-inverse" data-toggle="collapse" data-target=".nav-main-collapse" id="pull" style=""> <i class="fa fa-navicon"></i> </button>
    </div>
        
    <div class="col search search-desktop last">
      <div class="search-input" >
      <form action="//www.runoob.com/" target="_blank">
        <input class="placeholder" id="s" name="s" placeholder="搜索……"  autocomplete="off" style="height: 44px;">
      </form>
      
      </div>
    </div>
  </div>
</div>



<!-- 导航栏 -->
<!-- 导航栏 -->
<div class="container navigation">
	<div class="row">
		<div class="col nav">
			<ul class="pc-nav" id="runoob-detail-nav">
				<li><a href="../">首页</a></li>
				<li><a href="../html/html-tutorial.html">HTML</a></li>
				<li><a href="../css/css-tutorial.html">CSS</a></li>
				<li><a href="../js/js-tutorial.html">JavaScript</a></li>
				<li><a href="javascript:void(0);" data-id="vue">Vue</a></li>
				<li><a href="javascript:void(0);" data-id="bootstrap">Bootstrap</a></li>
				<li><a href="../nodejs/nodejs-tutorial.html">NodeJS</a></li>
				<li><a href="../python3/python3-tutorial.html">Python3</a></li>
				<li><a href="../python/python-tutorial.html">Python2</a></li>
				<li><a href="../java/java-tutorial.html">Java</a></li>
				<li><a href="../cprogramming/c-tutorial.html">C</a></li>
				<li><a href="../cplusplus/cpp-tutorial.html">C++</a></li>
				<li><a href="../csharp/csharp-tutorial.html">C#</a></li>
				<li><a href="../go/go-tutorial.html">Go</a></li>
				<li><a href="../sql/sql-tutorial.html">SQL</a></li>
				<li><a href="../linux/linux-tutorial.html">Linux</a></li>
				<li><a href="../jquery/jquery-tutorial.html">jQuery</a></li>
				<li><a href="../browser-history">本地书签</a></li>
				<!--
			
				<li><a href="../w3cnote/knowledge-start.html" style="font-weight: bold;" onclick="_hmt.push(['_trackEvent', '星球', 'click', 'start'])" title="我的圈子">我的圈子</a></li>				
				<li><a href="javascript:;" class="runoob-pop">登录</a></li>
				-->
      		</ul>
			<ul class="mobile-nav">
				<li><a href="../">首页</a></li>
				<li><a href="../html/html-tutorial.html">HTML</a></li>
				<li><a href="../css/css-tutorial.html">CSS</a></li>
				<li><a href="../js/js-tutorial.html">JS</a></li>
				<li><a href="../browser-history">本地书签</a></li>
				<li><a href="javascript:void(0)" class="search-reveal">Search</a> </li>
			</ul>
			
		</div>
	</div>
</div>

<div class="container sub-navigation sub-navigation-articles" style="display:none">
	<div class="row">
		<div class="col nav-sub">
		<ul id="python">
			<li class="cat-item"><a href="../python3/python3-tutorial.html">Python3 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../python/python-tutorial.html">Python2 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
		</ul>
		<ul id="vue">
			<li class="cat-item"><a href="../vue3/vue3-tutorial.html">Vue3 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../vue/vue-tutorial.html">vue2 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
		</ul>

		<ul id="bootstrap">
			<li class="cat-item"><a href="../bootstrap/bootstrap-tutorial.html">Bootstrap3 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../bootstrap4/bootstrap4-tutorial.html">Bootstrap4 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../bootstrap5/bootstrap5-tutorial.html">Bootstrap5 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
			<li class="cat-item"><a href="../bootstrap/bootstrap-v2-tutorial.html">Bootstrap2 教程 <i class="fa fa-external-link" aria-hidden="true"></i></a></li>
		</ul>
		</div>
	</div>
</div><!--  内容  -->
<div class="container main">
	<!-- 中间 -->
	<div class="row">
	
<div class="runoob-col-md2">
<div class="left-column">
	<div class="tab" style="overflow: hidden;text-overflow: ellipsis;white-space: nowrap;">
	<i class="fa fa-list" aria-hidden="true"></i> 
	<span>Python 3 教程</span>
	<a data-cate="124" href="javascript:void(0);" title="夜间模式"  id="moon"><i class="fa fa-moon-o" aria-hidden="true" style="line-height: 28px;font-size: 1.2em;margin: 2px 4px 0;"></i></a>
	<a data-cate="124" style="display:none;" href="javascript:void(0);" title="日间模式"  id="sun" ><i class="fa fa-sun-o" aria-hidden="true" style="line-height: 28px;font-size: 1.2em;margin: 2px 4px 0;line-height: 28px;
}"></i></a>

	</div>
	<div class="sidebar-box gallery-list">
		<div class="design" id="leftcolumn">
						<a target="_top" title="Python3 教程"  href="../python3/python3-tutorial.html" >
			Python3 教程			</a>
			<a target="_top" title="Python3 简介" href="python3-intro.html" > Python3 简介 </a>
<a target="_top" title="Python3 环境搭建" href="python3-install.html" > Python3 环境搭建</a>
<a target="_top" title="Python3 VScode" href="python-vscode-setup.html"> Python3 VScode </a>
<a target="_top" title="Python3 基础语法" href="python3-basic-syntax.html" > Python3 基础语法 </a>
<a target="_top" title="Python3 基本数据类型" href="python3-data-type.html" > Python3 基本数据类型 </a>
<a target="_top" title="Python3  数据类型转换" href="python3-type-conversion.html" > Python3 数据类型转换 </a>
			<a target="_top" title="Python3 解释器"  href="../python3/python3-interpreter.html" >
			Python3 解释器			</a>
						<a target="_top" title="Python3 注释"  href="../python3/python3-comment.html" >
			Python3 注释			</a>
			<a target="_top" title="Python3 运算符" href="python3-basic-operators.html"> Python3 运算符 </a>			<a target="_top" title="Python3 数字(Number)"  href="../python3/python3-number.html" >
			Python3 数字(Number)			</a>
						<a target="_top" title="Python3 字符串"  href="../python3/python3-string.html" >
			Python3 字符串			</a>
						<a target="_top" title="Python3 列表"  href="../python3/python3-list.html" >
			Python3 列表			</a>
			<a target="_top" title="Python3 元组" href="python3-tuple.html"> Python3 元组 </a>
<a target="_top" title="Python3 字典" href="python3-dictionary.html"> Python3 字典</a>
<a target="_top" title="Python3 集合" href="python3-set.html"> Python3 集合</a>			<a target="_top" title="Python3 条件控制"  href="../python3/python3-conditional-statements.html" >
			Python3 条件控制			</a>
						<a target="_top" title="Python3 循环语句"  href="../python3/python3-loop.html" >
			Python3 循环语句			</a>
			<a target="_top" title="Python3 编程第一步" href="python3-step1.html"> Python3 编程第一步</a>
<a target="_top" title="Python3 推导式" href="python-comprehensions.html" > Python3 推导式 </a>
<a target="_top" title="Python3 迭代器与生成器" href="python3-iterator-generator.html"> Python3 迭代器与生成器</a>			<a target="_top" title="Python3 函数"  href="../python3/python3-function.html" >
			Python3 函数			</a>
						<a target="_top" title="Python3 数据结构"  href="../python3/python3-data-structure.html" >
			Python3 数据结构			</a>
						<a target="_top" title="Python3 模块"  href="../python3/python3-module.html" >
			Python3 模块			</a>
						<a target="_top" title="Python3 输入和输出"  href="../python3/python3-inputoutput.html" >
			Python3 输入和输出			</a>
			<a target="_top" title="Python3 File" href="python3-file-methods.html"> Python3 File </a>
<a target="_top" title="Python3 OS" href="python3-os-file-methods.html"> Python3 OS </a>			<a target="_top" title="Python3 错误和异常"  href="../python3/python3-errors-execptions.html" >
			Python3 错误和异常			</a>
						<a target="_top" title="Python3 面向对象"  href="../python3/python3-class.html" >
			Python3 面向对象			</a>
			<a target="_top" title=" Python3 命名空间/作用域" href="../python3/python3-namespace-scope.html"> Python3 命名空间/作用域</a>			<a target="_top" title="Python3 标准库概览"  href="../python3/python3-stdlib.html" >
			Python3 标准库概览			</a>
						<a target="_top" title="Python3 实例"  href="../python3/python3-examples.html" >
			Python3 实例			</a>
			<a target="_blank" title="Python 测验" href="../quiz/python-quiz.html"> Python 测验 </a>
<br><h2 class="left"><span class="left_h2">Python3</span> 高级教程</h2>			<a target="_top" title="Python3 正则表达式"  href="../python3/python3-reg-expressions.html" >
			Python3 正则表达式			</a>
						<a target="_top" title="Python3 CGI编程"  href="../python3/python3-cgi-programming.html" >
			Python3 CGI编程			</a>
			<a target="_top" title="Python MySQL - mysql-connector 驱动" href="python-mysql-connector.html">Python3 MySQL(mysql-connector)</a>			<a target="_top" title="Python3 MySQL 数据库连接 &#8211; PyMySQL 驱动"  href="../python3/python3-mysql.html" >
			Python3 MySQL(PyMySQL)			</a>
						<a target="_top" title="Python3 网络编程"  href="../python3/python3-socket.html" >
			Python3 网络编程			</a>
						<a target="_top" title="Python3 SMTP发送邮件"  href="../python3/python3-smtp.html" >
			Python3 SMTP发送邮件			</a>
						<a target="_top" title="Python3 多线程"  href="../python3/python3-multithreading.html" >
			Python3 多线程			</a>
						<a target="_top" title="Python3 XML 解析"  href="../python3/python3-xml-processing.html" >
			Python3 XML 解析			</a>
						<a target="_top" title="Python3 JSON 数据解析"  href="../python3/python3-json.html" >
			Python3 JSON			</a>
						<a target="_top" title="Python3 日期和时间"  href="../python3/python3-date-time.html" >
			Python3 日期和时间			</a>
						<a target="_top" title="Python3 内置函数"  href="../python3/python3-built-in-functions.html" >
			Python3 内置函数			</a>
						<a target="_top" title="Python MongoDB"  href="../python3/python-mongodb.html" >
			Python3 MongoDB			</a>
			<a target="_top" title="Python3 urllib" href="python-urllib.html"> Python3 urllib</a>			<a target="_top" title="Python uWSGI  安装配置"  href="../python3/python-uwsgi.html" >
			Python uWSGI  安装配置			</a>
						<a target="_top" title="Python3 pip"  href="../python3/python3-pip.html" >
			Python3 pip			</a>
						<a target="_top" title="Python3 operator 模块"  href="../python3/python-operator.html" >
			Python3 operator			</a>
						<a target="_top" title="Python math 模块"  href="../python3/python-math.html" >
			Python math			</a>
						<a target="_top" title="Python requests 模块"  href="../python3/python-requests.html" >
			Python requests			</a>
						<a target="_top" title="Python random 模块"  href="../python3/python-random.html" >
			Python random			</a>
						<a target="_top" title="Python 有用的资源"  href="../python3/python3-resources.html" >
			Python 有用的资源			</a>
						<a target="_top" title="Python AI 绘画"  href="../python3/python-ai-draw.html" >
			Python AI 绘画			</a>
				
		</div>
	</div>	
</div>
</div>	<div class="col middle-column">
		
	
	<div class="article">
		
		<div class="previous-next-links">
			<div class="previous-design-link"><i style="font-size:16px;" class="fa fa-arrow-left" aria-hidden="true"></i> <a href="../python3/python3-loop.html" rel="prev"> Python3 循环语句</a> </div>
			<div class="next-design-link"><a href="../python3/python3-data-structure.html" rel="next"> Python3 数据结构</a> <i style="font-size:16px;" class="fa fa-arrow-right" aria-hidden="true"></i></div>
		</div>
<div class="article-heading-ad" style="display: block;">
<script async src=""
     crossorigin="anonymous"></script>
<!-- 728*90 固定 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:728px;height:90px"
     data-ad-client="ca-pub-5751451760833794"
     data-ad-slot="2147566827"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
		</div>
		<div class="article-body">
		
			<div class="article-intro" id="content">
			
			<h1>Python3 函数</h1>
<p>函数是组织好的，可重复使用的，用来实现单一，或相关联功能的代码段。</p><p>函数能提高应用的模块性，和代码的重复利用率。你已经知道Python提供了许多内建函数，比如print()。但你也可以自己创建函数，这被叫做用户自定义函数。</p>
<hr>
<h2>定义一个函数</h2>

<p>你可以定义一个由自己想要功能的函数，以下是简单的规则：</p>
<ul>
<li>函数代码块以 <b>def</b> 关键词开头，后接函数标识符名称和圆括号 <b>()</b>。</li>

<li>任何传入参数和自变量必须放在圆括号中间，圆括号之间可以用于定义参数。</li>

<li>函数的第一行语句可以选择性地使用文档字符串—用于存放函数说明。</li>

<li>函数内容以冒号 <span class="marked">:</span> 起始，并且缩进。</li>

<li><strong>return [表达式]</strong> 结束函数，选择性地返回一个值给调用方，不带表达式的 return 相当于返回 None。</li>
</ul>
<p><img decoding="async" src="https://www.runoob.com/wp-content/uploads/2014/05/py-tup-10-26-1.png"></p>
<hr>
<h3>语法</h3>
<p>Python 定义函数使用 def 关键字，一般格式如下：</p>
<pre>
def 函数名（参数列表）:
    函数体
</pre>

<p>默认情况下，参数值和参数名称是按函数声明中定义的顺序匹配起来的。</p>
<h3>实例</h3>
<p>让我们使用函数来输出"Hello World！"：</p>

<div class="example"> 
<div class="example_code">

<span style="color: #a50">#!/usr/bin/python3</span><br />
<br />
<span style="color: Green;font-weight:bold;">def</span> hello<span style="color: Olive;">&#40;</span><span style="color: Olive;">&#41;</span> :<br />
&nbsp; &nbsp; <span style="color: Green;font-weight:bold;">print</span><span style="color: Olive;">&#40;</span><span style="color: #a11;">&quot;Hello World!&quot;</span><span style="color: Olive;">&#41;</span><br />
<br />
hello<span style="color: Olive;">&#40;</span><span style="color: Olive;">&#41;</span><br />

</div>
</div>
<p>
更复杂点的应用，函数中带上参数变量:</p>

<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<p>比较两个数，并返回较大的数:</p>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-builtin">max</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">if</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> &gt; </span><span class="hl-identifier">b</span><span class="hl-code">:
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code">
    </span><span class="hl-reserved">else</span><span class="hl-code">:
        </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">b</span><span class="hl-code">
 
</span><span class="hl-identifier">a</span><span class="hl-code"> = </span><span class="hl-number">4</span><span class="hl-code">
</span><span class="hl-identifier">b</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-builtin">max</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-code">, </span><span class="hl-identifier">b</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
5
</pre>

<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<p>计算面积函数:</p>
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment"># 计算面积函数</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">area</span><span class="hl-brackets">(</span><span class="hl-identifier">width</span><span class="hl-code">, </span><span class="hl-identifier">height</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">width</span><span class="hl-code"> * </span><span class="hl-identifier">height</span><span class="hl-code">
 
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">print_welcome</span><span class="hl-brackets">(</span><span class="hl-identifier">name</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Welcome</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-identifier">print_welcome</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">Runoob</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">w</span><span class="hl-code"> = </span><span class="hl-number">4</span><span class="hl-code">
</span><span class="hl-identifier">h</span><span class="hl-code"> = </span><span class="hl-number">5</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">width =</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">w</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string"> height =</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">h</span><span class="hl-code">, </span><span class="hl-quotes">&quot;</span><span class="hl-string"> area =</span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">area</span><span class="hl-brackets">(</span><span class="hl-identifier">w</span><span class="hl-code">, </span><span class="hl-identifier">h</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
Welcome Runoob
width = 4  height = 5  area = 20
</pre>
<hr>
<h2>函数调用</h2>
<p>定义一个函数：给了函数一个名称，指定了函数里包含的参数，和代码块结构。</p>

<p>这个函数的基本结构完成以后，你可以通过另一个函数调用执行，也可以直接从 Python 命令提示符执行。</p>

<p>如下实例调用了 <strong>printme()</strong> 函数：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment"># 定义函数</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printme</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">str</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-comment"># 打印任何传入的字符串</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">str</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用函数</span><span class="hl-code">
</span><span class="hl-identifier">printme</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">我要调用用户自定义函数!</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">printme</span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">再次调用同一函数</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
我要调用用户自定义函数!
再次调用同一函数
</pre>
<hr><h2>参数传递</h2>
<p>在 python 中，类型属于对象，对象有不同类型的区分，变量是没有类型的：</p>
<pre>
a=[1,2,3]

a="Runoob"
</pre>
<p>以上代码中，<b>[1,2,3]</b> 是 List 类型，<b>"Runoob"</b> 是 String 类型，而变量 a 是没有类型，她仅仅是一个对象的引用（一个指针），可以是指向 List 类型对象，也可以是指向 String 类型对象。</p>
<h3>可更改(mutable)与不可更改(immutable)对象</h3>
<p>在 python 中，strings, tuples, 和 numbers 是不可更改的对象，而 list,dict 等则是可以修改的对象。</p>
<ul><li><p><b>
不可变类型：</b>变量赋值 <b>a=5</b> 后再赋值 <b>a=10</b>，这里实际是新生成一个 int 值对象 10，再让 a 指向它，而 5 被丢弃，不是改变 a 的值，相当于新生成了 a。</p></li><li><p><b>
可变类型：</b>变量赋值 <b>la=[1,2,3,4]</b> 后再赋值 <b>la[2]=5</b> 则是将 list la 的第三个元素值更改，本身la没有动，只是其内部的一部分值被修改了。</p></li></ul>
<p>python 函数的参数传递：</p>
<ul><li><p><b>
不可变类型：</b>类似 C++ 的值传递，如整数、字符串、元组。如 fun(a)，传递的只是 a 的值，没有影响 a 对象本身。如果在 fun(a) 内部修改 a 的值，则是新生成一个 a 的对象。</p></li><li><p><b>
可变类型：</b>类似 C++ 的引用传递，如 列表，字典。如 fun(la)，则是将 la 真正的传过去，修改后 fun 外部的 la 也会受影响</p></li></ul>
<p>python 中一切都是对象，严格意义我们不能说值传递还是引用传递，我们应该说传不可变对象和传可变对象。</p>
<h3>python 传不可变对象实例</h3>

<p>通过 <strong>id()</strong> 函数来查看内存地址变化：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">change</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-code">:
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-builtin">id</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">   </span><span class="hl-comment"># 指向的是同一个对象</span><span class="hl-code">
    </span><span class="hl-identifier">a</span><span class="hl-code">=</span><span class="hl-number">10</span><span class="hl-code">
    </span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-builtin">id</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">   </span><span class="hl-comment"># 一个新对象</span><span class="hl-code">
 
</span><span class="hl-identifier">a</span><span class="hl-code">=</span><span class="hl-number">1</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-builtin">id</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">change</span><span class="hl-brackets">(</span><span class="hl-identifier">a</span><span class="hl-brackets">)</span></div>
</div>
</div><p>
以上实例输出结果为：</p>
<pre>4379369136
4379369136
4379369424</pre><p>可以看见在调用函数前后，形参和实参指向的是同一个对象（对象 id 相同），在函数内部修改形参后，形参指向的是不同的 id。</p>
<h3>传可变对象实例</h3>


<p>可变对象在函数里修改了参数，那么在调用这个函数的函数里，原始的参数也被改变了。例如：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment"># 可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">changeme</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">mylist</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">修改传入的列表</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">mylist</span><span class="hl-code">.</span><span class="hl-identifier">append</span><span class="hl-brackets">(</span><span class="hl-brackets">[</span><span class="hl-number">1</span><span class="hl-code">,</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-number">3</span><span class="hl-code">,</span><span class="hl-number">4</span><span class="hl-brackets">]</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">函数内取值: </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">mylist</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用changeme函数</span><span class="hl-code">
</span><span class="hl-identifier">mylist</span><span class="hl-code"> = </span><span class="hl-brackets">[</span><span class="hl-number">10</span><span class="hl-code">,</span><span class="hl-number">20</span><span class="hl-code">,</span><span class="hl-number">30</span><span class="hl-brackets">]</span><span class="hl-code">
</span><span class="hl-identifier">changeme</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">mylist</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">函数外取值: </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">mylist</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>传入函数的和在末尾添加新内容的对象用的是同一个引用。故输出结果如下：</p>
<pre>
函数内取值:  [10, 20, 30, [1, 2, 3, 4]]
函数外取值:  [10, 20, 30, [1, 2, 3, 4]]
</pre><hr>
<h2>参数</h2>
<p>以下是调用函数时可使用的正式参数类型：</p>
<ul>
<li>必需参数</li>

<li>关键字参数</li>

<li>默认参数</li>

<li>不定长参数</li>
</ul>
<h3>必需参数</h3>

<p>必需参数须以正确的顺序传入函数。调用时的数量必须和声明时的一样。</p>

<p>调用 printme() 函数，你必须传入一个参数，不然会出现语法错误：</p>

<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment">#可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printme</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">str</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">打印任何传入的字符串</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">str</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用 printme 函数，不加参数会报错</span><span class="hl-code">
</span><span class="hl-identifier">printme</span><span class="hl-brackets">(</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
Traceback (most recent call last):
  File "test.py", line 10, in &lt;module&gt;
    printme()
TypeError: printme() missing 1 required positional argument: 'str'
</pre>
<h3>关键字参数</h3>

<p>关键字参数和函数调用关系紧密，函数调用使用关键字参数来确定传入的参数值。</p>
<p>使用关键字参数允许函数调用时参数的顺序与声明时不一致，因为 Python 解释器能够用参数名匹配参数值。</p>
<p>以下实例在函数 printme() 调用时使用参数名：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment">#可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printme</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">str</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">打印任何传入的字符串</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">str</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code">
 
</span><span class="hl-comment">#调用printme函数</span><span class="hl-code">
</span><span class="hl-identifier">printme</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">str</span><span class="hl-code"> = </span><span class="hl-quotes">&quot;</span><span class="hl-string">菜鸟教程</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：
</p>
<pre>
菜鸟教程
</pre>
<p>以下实例中演示了函数参数的使用不需要使用指定顺序：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment">#可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">age</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">打印任何传入的字符串</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">名字: </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">年龄: </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">age</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code">
 
</span><span class="hl-comment">#调用printinfo函数</span><span class="hl-code">
</span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">=</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">runoob</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
名字:  runoob
年龄:  50
</pre>
<h3>默认参数</h3>

<p>调用函数时，如果没有传递参数，则会使用默认参数。以下实例中如果没有传入 age 参数，则使用默认值：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment">#可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">, </span><span class="hl-identifier">age</span><span class="hl-code"> = </span><span class="hl-number">35</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">打印任何传入的字符串</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">名字: </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">年龄: </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">age</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code">
 
</span><span class="hl-comment">#调用printinfo函数</span><span class="hl-code">
</span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">age</span><span class="hl-code">=</span><span class="hl-number">50</span><span class="hl-code">, </span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">runoob</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">------------------------</span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">name</span><span class="hl-code">=</span><span class="hl-quotes">&quot;</span><span class="hl-string">runoob</span><span class="hl-quotes">&quot;</span><span class="hl-code"> </span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
名字:  runoob
年龄:  50
------------------------
名字:  runoob
年龄:  35
</pre>
<h3>不定长参数</h3>

<p>你可能需要一个函数能处理比当初声明时更多的参数。这些参数叫做不定长参数，和上述 2 种参数不同，声明时不会命名。基本语法如下：</p>
<pre>
def functionname([formal_args,] *var_args_tuple ):
   "函数_文档字符串"
   function_suite
   return [expression]
</pre>
<p>加了星号 <span class="marked">*</span> 的参数会以元组(tuple)的形式导入，存放所有未命名的变量参数。</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
  
</span><span class="hl-comment"># 可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">arg1</span><span class="hl-code">, *</span><span class="hl-identifier">vartuple</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">打印任何传入的参数</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">输出: </span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">arg1</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">vartuple</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用printinfo 函数</span><span class="hl-code">
</span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">70</span><span class="hl-code">, </span><span class="hl-number">60</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code"> </span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>输出: 
70
(60, 50)</pre>


</p>如果在函数调用时没有指定参数，它就是一个空元组。我们也可以不向函数传递未命名的变量。如下实例：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment"># 可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">arg1</span><span class="hl-code">, *</span><span class="hl-identifier">vartuple</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">打印任何传入的参数</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">输出: </span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">arg1</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">for</span><span class="hl-code"> </span><span class="hl-identifier">var</span><span class="hl-code"> </span><span class="hl-reserved">in</span><span class="hl-code"> </span><span class="hl-identifier">vartuple</span><span class="hl-code">:
      </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">var</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用printinfo 函数</span><span class="hl-code">
</span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">10</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">70</span><span class="hl-code">, </span><span class="hl-number">60</span><span class="hl-code">, </span><span class="hl-number">50</span><span class="hl-code"> </span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
输出:
10
输出:
70
60
50
</pre><p>
还有一种就是参数带两个星号 <span class="marked">**</span>基本语法如下：</p>
<pre>
def functionname([formal_args,] **var_args_dict ):
   "函数_文档字符串"
   function_suite
   return [expression]
</pre><p>加了两个星号 <span class="marked">**</span> 的参数会以字典的形式导入。</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
  
</span><span class="hl-comment"># 可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">arg1</span><span class="hl-code">, **</span><span class="hl-identifier">vardict</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-quotes">&quot;</span><span class="hl-string">打印任何传入的参数</span><span class="hl-quotes">&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">输出: </span><span class="hl-quotes">&quot;</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">arg1</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-identifier">vardict</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用printinfo 函数</span><span class="hl-code">
</span><span class="hl-identifier">printinfo</span><span class="hl-brackets">(</span><span class="hl-number">1</span><span class="hl-code">, </span><span class="hl-identifier">a</span><span class="hl-code">=</span><span class="hl-number">2</span><span class="hl-code">,</span><span class="hl-identifier">b</span><span class="hl-code">=</span><span class="hl-number">3</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
输出: 
1
{'a': 2, 'b': 3}
</pre>
<p>声明函数时，参数中星号 <span class="marked">*</span> 可以单独出现，例如:</p>

<pre>def f(a,b,*,c):
    return a+b+c</pre>

<p>如果单独出现星号 <span class="marked">*</span>，则星号 <span class="marked">*</span> 后的参数必须用关键字传入：</p>

<pre>&gt;&gt;&gt; def f(a,b,*,c):
...     return a+b+c
... 
&gt;&gt;&gt; f(1,2,3)   # 报错
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
TypeError: f() takes 2 positional arguments but 3 were given
&gt;&gt;&gt; f(1,2,c=3) # 正常
6
&gt;&gt;&gt;</pre>

<hr><h2>匿名函数</h2>

<p>Python 使用 <span class="marked">lambda</span> 来创建匿名函数。</p>
<p>所谓匿名，意即不再使用 <strong>def</strong> 语句这样标准的形式定义一个函数。</p>
<ul>

<li><span class="marked">lambda</span> 只是一个表达式，函数体比 <strong>def</strong> 简单很多。
</li><li>
lambda 的主体是一个表达式，而不是一个代码块。仅仅能在 lambda 表达式中封装有限的逻辑进去。
</li><li>lambda 函数拥有自己的命名空间，且不能访问自己参数列表之外或全局命名空间里的参数。</li>

<li>虽然 lambda 函数看起来只能写一行，却不等同于 C 或 C++ 的内联函数，后者的目的是调用小函数时不占用栈内存从而增加运行效率。</li>
</ul>
<h3>语法</h3>

<p>lambda 函数的语法只包含一个语句，如下：</p>
<pre>
lambda [arg1 [,arg2,.....argn]]:expression
</pre>
<p>设置参数 a 加上 10:</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-identifier">x</span><span class="hl-code"> = </span><span class="hl-reserved">lambda</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> : </span><span class="hl-identifier">a</span><span class="hl-code"> + </span><span class="hl-number">10</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">x</span><span class="hl-brackets">(</span><span class="hl-number">5</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span></div></div></div>
<p>以上实例输出结果：</p>
<pre>15</pre>
<p>以下实例匿名函数设置两个参数：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment"># 可写函数说明</span><span class="hl-code">
</span><span class="hl-identifier">sum</span><span class="hl-code"> = </span><span class="hl-reserved">lambda</span><span class="hl-code"> </span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-identifier">arg2</span><span class="hl-code">: </span><span class="hl-identifier">arg1</span><span class="hl-code"> + </span><span class="hl-identifier">arg2</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用sum函数</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">相加后的值为 : </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-builtin">sum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">20</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">相加后的值为 : </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-builtin">sum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">20</span><span class="hl-code">, </span><span class="hl-number">20</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
相加后的值为 :  30
相加后的值为 :  40
</pre>
<p>我们可以将匿名函数封装在一个函数内，这样可以使用同样的代码来创建多个匿名函数。</p>
<p>以下实例将匿名函数封装在 myfunc 函数中，通过传入不同的参数来创建不同的匿名函数：</p>
<div class="example"><h2 class="example">实例</h2> <div class="example_code"><div class="hl-main"><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-identifier">myfunc</span><span class="hl-brackets">(</span><span class="hl-identifier">n</span><span class="hl-brackets">)</span><span class="hl-code">:
  </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-reserved">lambda</span><span class="hl-code"> </span><span class="hl-identifier">a</span><span class="hl-code"> : </span><span class="hl-identifier">a</span><span class="hl-code"> * </span><span class="hl-identifier">n</span><span class="hl-code">
 
</span><span class="hl-identifier">mydoubler</span><span class="hl-code"> = </span><span class="hl-identifier">myfunc</span><span class="hl-brackets">(</span><span class="hl-number">2</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">mytripler</span><span class="hl-code"> = </span><span class="hl-identifier">myfunc</span><span class="hl-brackets">(</span><span class="hl-number">3</span><span class="hl-brackets">)</span><span class="hl-code">
 
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">mydoubler</span><span class="hl-brackets">(</span><span class="hl-number">11</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-brackets">(</span><span class="hl-identifier">mytripler</span><span class="hl-brackets">(</span><span class="hl-number">11</span><span class="hl-brackets">)</span><span class="hl-brackets">)</span></div></div></div>
<p>以上实例输出结果：</p>
<pre>
22
33
</pre>
<hr>
<h2>return 语句</h2>

<p><b>return [表达式]</b> 语句用于退出函数，选择性地向调用方返回一个表达式。不带参数值的 return 语句返回 None。之前的例子都没有示范如何返回数值，以下实例演示了 return 语句的用法：</p>
<div class="example"> 
<h2 class="example">实例(Python 3.0+)</h2> 
<div class="example_code">
<div class="hl-main"><span class="hl-comment">#!/usr/bin/python3</span><span class="hl-code">
 
</span><span class="hl-comment"># 可写函数说明</span><span class="hl-code">
</span><span class="hl-reserved">def</span><span class="hl-code"> </span><span class="hl-builtin">sum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-identifier">arg1</span><span class="hl-code">, </span><span class="hl-identifier">arg2</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">:
   </span><span class="hl-comment"># 返回2个参数的和.&quot;</span><span class="hl-code">
   </span><span class="hl-identifier">total</span><span class="hl-code"> = </span><span class="hl-identifier">arg1</span><span class="hl-code"> + </span><span class="hl-identifier">arg2</span><span class="hl-code">
   </span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">函数内 : </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">total</span><span class="hl-brackets">)</span><span class="hl-code">
   </span><span class="hl-reserved">return</span><span class="hl-code"> </span><span class="hl-identifier">total</span><span class="hl-code">
 
</span><span class="hl-comment"># 调用sum函数</span><span class="hl-code">
</span><span class="hl-identifier">total</span><span class="hl-code"> = </span><span class="hl-builtin">sum</span><span class="hl-brackets">(</span><span class="hl-code"> </span><span class="hl-number">10</span><span class="hl-code">, </span><span class="hl-number">20</span><span class="hl-code"> </span><span class="hl-brackets">)</span><span class="hl-code">
</span><span class="hl-identifier">print</span><span class="hl-code"> </span><span class="hl-brackets">(</span><span class="hl-quotes">&quot;</span><span class="hl-string">函数外 : </span><span class="hl-quotes">&quot;</span><span class="hl-code">, </span><span class="hl-identifier">total</span><span class="hl-brackets">)</span></div>
</div>
</div>
<p>以上实例输出结果：</p>
<pre>
函数内 :  30
函数外 :  30
</pre>
<hr>
<h2>强制位置参数</h2>
<p>Python3.8 新增了一个函数形参语法 / 用来指明函数形参必须使用指定位置参数，不能使用关键字参数的形式。</p>

在以下的例子中，形参 a 和 b 必须使用指定位置参数，c 或 d 可以是位置形参或关键字形参，而 e 和 f 要求为关键字形参:

<pre>def f(a, b, /, c, d, *, e, f):
    print(a, b, c, d, e, f)</pre>
<p>以下使用方法是正确的:</p>

<pre>f(10, 20, 30, d=40, e=50, f=60)</pre>
<p>以下使用方法会发生错误:</p>

<pre>f(10, b=20, c=30, d=40, e=50, f=60)   # b 不能使用关键字参数的形式
f(10, 20, 30, 40, 50, f=60)           # e 必须使用关键字参数的形式</pre>			<!-- 其他扩展 -->
			<hr>
<pre id="py-qa-1" style="display:none;">x = True
def printLine(text):
    print(text, ' Runoob')
printLine('Python')
</pre>
<pre id="py-qa-2" style="display:none;">
def greetPerson(*name):
    print('Hello', name)
  
greetPerson('Runoob', 'Google')
</pre>
<pre id="py-qa-3" style="display:none;">
result = lambda x: x * x
print(result(5))
</pre>
<pre id="py-qa-4" style="display:none;">
def Foo(x):
    if (x==1):
        return 1
    else:
        return x+Foo(x-1)

print(Foo(4))
</pre>
<pre id="py-qa-5" style="display:none;">
numbers = [1, 3, 6]
newNumbers = tuple(map(lambda x: x , numbers))
print(newNumbers)
</pre>
<div id="quiz">
  <div id="quiz-start-screen">
    <p><a href="#" id="quiz-start-btn" class="quiz-button">课后练习</a></p>
  </div>
</div>
<script>
$(function() {
code1= '<pre>' + $("#py-qa-1").html() + '</pre>';
code2= '<pre>' + $("#py-qa-2").html() + '</pre>';
code3= '<pre>' + $("#py-qa-3").html() + '</pre>';
code4= '<pre>' + $("#py-qa-4").html() + '</pre>';
code5= '<pre>' + $("#py-qa-5").html() + '</pre>';
  $('#quiz').quiz({
    counterFormat: '第 %current 题，共 %total 题',
    resultsFormat: '回答正确 %score 题，总共 %total 题！',
    nextButtonText: '下一题',
    finishButtonText: '完成',
    restartButtonText: '重新测验',
    questions: [
      {
        'q':  '<p>以下内容关于函数描述正确的是？</p>',
        'options': [
          '函数用于创建对象',
          '函数可以让代码执行的更快',
          '函数是一段代码用于执行特定的任务',
          '以上说法都是正确的'
        ],
        'correctIndex': 2,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，函数是一段代码用于执行特定的任务。'
      },
      {
        'q': code1 + '<p>以上代码输出结果为？</p>',
        'options': [
          'Python',
          'Python Runoob',
          'text Runoob',
          'Runoob'
        ],
        'correctIndex': 1,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，正确的是 Python Runoob。'
      },
      {
        'q':  '<p>如果函数没有使用 return 语句，则函数返回的是？</p>',
        'options': [
          '0',
          'None 对象',
          '任意的整数',
          '错误! 函数必须要有返回值。'
        ],
        'correctIndex': 1,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，正确的是 None 对象。'
      },
      {
        'q': code2 + '<p>以上代码输出结果为？</p>',
        'options': [
          'Hello Runoob<br>Hello Google',
          'Hello (\'Runoob\', \'Google\')',
          'Hello Runoob',
          '错误！函数只能接收一个参数。'
        ],
        'correctIndex': 1,
        'correctResponse': '回答正确，加了星号 * 的参数会以元组(tuple)的形式导入，存放所有未命名的变量参数。',
        'incorrectResponse': '回答错误，正确的为第二个选项，加了星号 * 的参数会以元组(tuple)的形式导入，存放所有未命名的变量参数。'
      },
      {
        'q':  '<p>关于递归函数描述正确的是？</p>',
        'options': [
          '递归函数可以调用程序的使用函数。',
          '递归函数用于调用函数的本身。',
          '递归函数除了函数本身，可以调用程序的其他所有函数。',
          'Python 中没有递归函数。'
        ],
        'correctIndex': 1,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，递归函数用于调用函数的本身。'
      },
      {
        'q': code3 + '<p>以上代码输出结果为？</p>',
        'options': [
          'lambda x: x*x',
          '10',
          '25',
          '5*5'
        ],
        'correctIndex': 2,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，正确为 25。'
      },
      {
        'q': code4 + '<p>以上代码输出结果为？</p>',
        'options': [
          '10',
          '24',
          '7',
          '1'
        ],
        'correctIndex': 0,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，正确为 10。'
      },
      {
        'q':  '<p>如果我们需要从 math 模块中输出 pi 常量，以下代码正确的是？</p>',
        'options': [
          'print(math.pi)',
          'print(pi)',
          'from math import pi<br>print(pi)',
          'from math import pi<br>print(math.pi)'
        ],
        'correctIndex': 2,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，正确为第三个选项。'
      },
      {
        'q':  '<p>以下哪个符号用于从包中导入模块？</p>',
        'options': [
          '<strong>.</strong>',
          '<strong>*</strong>',
          '<strong>-&gt;</strong>',
          '<strong>,</strong>'
        ],
        'correctIndex': 0,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，正确为点号 .'
      },
      {
        'q': code5 + '<p>以上代码输出结果为？</p>',
        'options': [
          '[1, 3, 6]',
          '(1, 3, 6)',
          '[2, 6, 12]',
          '(2, 6, 12)'
        ],
        'correctIndex': 1,
        'correctResponse': '回答正确。',
        'incorrectResponse': '回答错误，正确为 (1, 3, 6)。'
      },
    ]
  });
})
</script>			
			</div>
			
		</div>
		
		<div class="previous-next-links">
			<div class="previous-design-link"><i style="font-size:16px;" class="fa fa-arrow-left" aria-hidden="true"></i> <a href="../python3/python3-loop.html" rel="prev"> Python3 循环语句</a> </div>
			<div class="next-design-link"><a href="../python3/python3-data-structure.html" rel="next"> Python3 数据结构</a> <i style="font-size:16px;" class="fa fa-arrow-right" aria-hidden="true"></i></div>
		</div>
		<!-- 笔记列表 -->
		<style>
.wrapper {
  /*text-transform: uppercase; */
  background: #ececec;
  color: #555;
  cursor: help;
  font-family: "Gill Sans", Impact, sans-serif;
  font-size: 20px;
  position: relative;
  text-align: center;
  width: 200px;
  -webkit-transform: translateZ(0); /* webkit flicker fix */
  -webkit-font-smoothing: antialiased; /* webkit text rendering fix */
}

.wrapper .tooltip {
  white-space: nowrap;
  font-size: 14px;
  text-align: left;
  background: #96b97d;
  bottom: 100%;
  color: #fff;
  display: block;
  left: -25px;
  margin-bottom: 15px;
  opacity: 0;
  padding: 14px;
  pointer-events: none;
  position: absolute;
  
  -webkit-transform: translateY(10px);
     -moz-transform: translateY(10px);
      -ms-transform: translateY(10px);
       -o-transform: translateY(10px);
          transform: translateY(10px);
  -webkit-transition: all .25s ease-out;
     -moz-transition: all .25s ease-out;
      -ms-transition: all .25s ease-out;
       -o-transition: all .25s ease-out;
          transition: all .25s ease-out;
  -webkit-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
     -moz-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
      -ms-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
       -o-box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
          box-shadow: 2px 2px 6px rgba(0, 0, 0, 0.28);
}
.tooltip a {
	color:#fff;
}
/* This bridges the gap so you can mouse into the tooltip without it disappearing */
.wrapper .tooltip:before {
  bottom: -20px;
  content: " ";
  display: block;
  height: 20px;
  left: 0;
  position: absolute;
  width: 100%;
}  

/* CSS Triangles - see Trevor's post */
.wrapper .tooltip:after {
  border-left: solid transparent 10px;
  border-right: solid transparent 10px;
  border-top: solid #96b97d 10px;
  bottom: -10px;
  content: " ";
  height: 0;
  left: 20%;
  margin-left: -13px;
  position: absolute;
  width: 0;
}
.wrapper .tooltip1 {
	margin-left: 50px;
	padding-top: 0px;
}
/*
.wrapper:hover .tooltip {
  opacity: 1;
  pointer-events: auto;
  -webkit-transform: translateY(0px);
     -moz-transform: translateY(0px);
      -ms-transform: translateY(0px);
       -o-transform: translateY(0px);
          transform: translateY(0px);
}
*/
/* IE can just show/hide with no transition */
.lte8 .wrapper .tooltip {
  display: none;
}

.lte8 .wrapper:hover .tooltip {
  display: block;
}

</style>

<link rel="stylesheet" href="../0/upvotejs.css">
<script src=""></script>
<script src=""></script>
<div class="title" id="comments">
	<h2 class="">
    <div class="altblock">
				<i style="font-size:28px;margin-top: 8px;" class="fa fa-plus-square" aria-hidden="true"></i>
		    </div>
    <span class="mw-headline" id="qa_headline">20  篇笔记</span>
	<span class="mw-headline" id="user_add_note" style="float:right;line-height: 62px;padding-right: 14px;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i>  写笔记</span>
    </h2>
</div>

<div id="postcomments"  style="display:none;" >
	<ol class="commentlist">
		<li class="comment even thread-even depth-1" id="comment-15519"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;不跟随</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;368***608@qq.com</p></div><div id="runoobvote-id-15519" data-commid = "15519" class="upvotejs"><a class="upvote"></a> <span class="count">328</span></div></div><div class="comt-main" id="div-comment-15519"><p>默认参数必须放在最后面，否则会报：</p>
<pre>
SyntaxError: non-default argument follows default argument
</pre><pre>
# 可写函数说明
def printinfo( age=35,name):   # 默认参数不在最后，会报错
    "打印任何传入的字符串"
    print("名字: ", name)
    print("年龄: ", age)
    return</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">不跟随</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;不跟随</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;368***608@qq.com</p></div></span>6年前 (2017-07-28)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-15532"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;夏老爷</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;112***6553@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="http://www.cnblogs.com/dukeidea/p/6396980.html">&nbsp;&nbsp;参考地址</a></p></div><div id="runoobvote-id-15532" data-commid = "15532" class="upvotejs"><a class="upvote"></a> <span class="count">97</span></div></div><div class="comt-main" id="div-comment-15532"><p><strong>def(**kwargs)</strong> 把N个关键字参数转化为字典:</p>
<pre>
&gt;&gt;&gt; def func(country,province,**kwargs):
...     print(country,province,kwargs)
... 
&gt;&gt;&gt; func("China","Sichuan",city = "Chengdu", section = "JingJiang")
China Sichuan {'city': 'Chengdu', 'section': 'JingJiang'}
&gt;&gt;&gt; 
</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">夏老爷</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;夏老爷</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;112***6553@qq.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="http://www.cnblogs.com/dukeidea/p/6396980.html">&nbsp;&nbsp;参考地址</a></p></div></span>6年前 (2017-07-28)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-16344"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;dessertfox</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;che***anren@tju.edu.cn</p></div><div id="runoobvote-id-16344" data-commid = "16344" class="upvotejs"><a class="upvote"></a> <span class="count">135</span></div></div><div class="comt-main" id="div-comment-16344"><p>lambda 匿名函数也是可以使用"<strong>关键字参数</strong>"进行参数传递</p>

<pre>&gt;&gt;&gt; g= lambda x,y : x**2+y**2
&gt;&gt;&gt; g(2,3)
13
&gt;&gt;&gt; g(y=3,x=2)
13</pre>
<p>同样地，lambda 匿名函数也可以设定默认值</p>

<pre>&gt;&gt;&gt; g= lambda x=0,y=0 : x**2+y**2
&gt;&gt;&gt; g(2,3)
13
&gt;&gt;&gt; g(2)
4
&gt;&gt;&gt; g(y=3)
9</pre>
<p><strong>注意：</strong>如果只打算给其中一部分参数设定默认值，那么应当将其放在靠后的位置（和定义函数时一样，避免歧义），否则会报错。</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">dessertfox</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;dessertfox</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;che***anren@tju.edu.cn</p></div></span>6年前 (2017-08-14)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-20599"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Mr.Wang</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;992***591@qq.com</p></div><div id="runoobvote-id-20599" data-commid = "20599" class="upvotejs"><a class="upvote"></a> <span class="count">123</span></div></div><div class="comt-main" id="div-comment-20599"><p>关于可更改与不可更改类型， 以及其它语言的值类型与引用类型的介绍，一直一来感觉都不太严谨， 说法是否正确有待验证。</p>

<p>简单的说就是，不可更改类型传到函数里重新赋值后，两次输出值不一样，而可更改类型传到函数里对对象的"属性" 重新赋值后输出值一样。</p>

<p>这里照搬一下例子：</p>

<pre># 可写函数说明
def changeme( mylist ):
   "修改传入的列表"
   mylist.append([1,2,3,4])
   print ("函数内取值: ", mylist)
   return
 
# 调用changeme函数
mylist = [10,20,30]
changeme( mylist )
print ("函数外取值: ", mylist)</pre>
<p>请注意：上面特意用了引号标准的部分，对可变类型或者引用的操作修改的是传过来的对象的属性。</p>

<p>可以这么理解(例子有点随意)：我在画画，小明来了说他也要画，我让他和我一起画，他如果和我在同一个画板上画，那么我们两的画就会同时改变。 而如果他说不，我要另外用一块画板，然后重新拿了块画板画起来了，那么我们两的画自然就不一样了。</p>

<p>同理可更改类型 的属性进行操作，这只是对引用的内存块里面的值进行操作，引用并没变，自然所有引用它的对象的值都变了。而对不可更改的对象进行操作，因为它引用的内存块只是对应一个固定的值，不能进行修改，要重新复制实际上就是更新引用。</p>

<p>如果我们运行下面的例子，对可更改类型的引用进行修改，结果就不一样了。</p>


<pre># 可写函数说明
def changeme( mylist ):
   "修改传入的列表"
   mylist = [1,2,3,4]
   print ("函数内取值: ", mylist)
   return
 
# 调用changeme函数
mylist = [10,20,30]
changeme( mylist )
print ("函数外取值: ", mylist)</pre>
<p>结果</p>

<pre>函数内取值:  [1, 2, 3, 4]
函数外取值:  [10, 20, 30]</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">Mr.Wang</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Mr.Wang</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;992***591@qq.com</p></div></span>6年前 (2017-11-21)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-21605"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Rosevil1874</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;157***11424@163.com</p></div><div id="runoobvote-id-21605" data-commid = "21605" class="upvotejs"><a class="upvote"></a> <span class="count">148</span></div></div><div class="comt-main" id="div-comment-21605"><p>对于变量作用域，变量的访问以 <strong>L（Local） –> E（Enclosing） –> G（Global） –>B（Built-in）</strong> 的规则查找，即：在局部找不到，便会去局部外的局部找（例如闭包），再找不到就会去全局找，再者去内建中找。</p>

<p>观察以下几个例子，均从内部函数输出变量 x：</p>

<p><strong>1. 局部作用域</strong></p>

<pre>x = int(3.3)

x = 0
def outer():
    x = 1
    def inner():
        x = 2
        print(x)
    inner()

outer()</pre>
<p>执行结果为 2，因为此时直接在函数 inner 内部找到了变量 x。</p>

<p><strong>2.闭包函数外的函数中</strong></p>

<pre>x = int(3.3)

x = 0
def outer():
    x = 1
    def inner():
        i = 2
        print(x)
    inner()

outer()</pre>
<p>执行结果为 1，因为在内部函数 inner 中找不到变量 x，继续去局部外的局部——函数 outer 中找，这时找到了，输出 1。</p>

<p><strong>3.全局作用域</strong></p>

<pre>x = int(3.3)
x = 0
def outer():
    o = 1
    def inner():
        i = 2
        print(x)
    inner()

outer()</pre>
<p>执行结果为 0，在局部（inner函数）、局部的局部（outer函数）都没找到变量 x，于是访问全局变量，此时找到了并输出。</p>

<p><strong>4. 内建作用域</strong></p>

<pre>x = int(3.3)
g = 0
def outer():
    o = 1
    def inner():
        i = 2
        print(x)
    inner()

outer()</pre>
<p>执行结果为 3，在局部（inner函数）、局部的局部（outer函数）以及全局变量中都没有找到变量x，于是访问内建变量，此时找到了并输出。</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">Rosevil1874</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Rosevil1874</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;157***11424@163.com</p></div></span>5年前 (2017-12-13)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-22272"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;ls</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;moo***huan@foxmail.com</p></div><div id="runoobvote-id-22272" data-commid = "22272" class="upvotejs"><a class="upvote"></a> <span class="count">84</span></div></div><div class="comt-main" id="div-comment-22272"><p><strong>函数内可以访问全局变量，但不能更新(修改)其值！</strong></p>

<p>例 :</p>

<pre>a = 10
def sum ( n ) :
   n += a
   print ('a = ', a, end = ' , ' )
   print ( 'n = ', n )
  
sum(3)</pre>
<p>输出 :</p>

<pre>a =  10 , n =  13</pre>
<p>如果引用了还没更新的值则会报错 :</p>

<pre>a = 10
def sum ( n ) :
   n += a
   a = 11
   print ('a = ', a, end = ' , ' )
   print ( 'n = ', n )
  
sum(3)</pre>
<p>输出 :</p>

...
<pre>UnboundLocalError: local variable 'a' referenced before assignment</pre>
<p>可以加上 global 引用以更新变量值 :</p>

<pre>a = 10
def sum ( n ) :
   global a
   n += a
   a = 11
   print ('a = ', a, end = ' , ' )
   print ( 'n = ', n )

sum ( 3 )
print ( '外 a = ', a )</pre>
<p>输出:</p>

<p>a =  11 , n =  13
外 a = 11</p>
<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">ls</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;ls</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;moo***huan@foxmail.com</p></div></span>5年前 (2017-12-28)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-24169"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;FVortex</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;hty***80402@qq.com</p></div><div id="runoobvote-id-24169" data-commid = "24169" class="upvotejs"><a class="upvote"></a> <span class="count">52</span></div></div><div class="comt-main" id="div-comment-24169"><p>函数也可以以一个函数为其参数:</p>

<pre>def hello () :
  print ("Hello, world!")

def execute(f):
  "执行一个没有参数的函数"
  f()

execute(hello)</pre>

<p>输出：</p>

<pre>Hello, world!</pre>
<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">FVortex</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;FVortex</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;hty***80402@qq.com</p></div></span>5年前 (2018-02-16)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-29888"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Mr.Wu</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;928***263@qq.com</p></div><div id="runoobvote-id-29888" data-commid = "29888" class="upvotejs"><a class="upvote"></a> <span class="count">64</span></div></div><div class="comt-main" id="div-comment-29888"><p>可以通过  <span class="marked">函数名.__doc__</span> 的方式来显示函数的说明文档，感觉这个如果在阅读比较大的程序时应该会有用，同时也在提示自己在写函数时注意添加文档说明。</p>

<pre>def add(a,b):
    "这是 add 函数文档"
    return a+b

print (add.__doc__)</pre>

<p>输出结果为：</p>
<pre>这是 add 函数文档</pre>



<blockquote><p>更多内容可参考：<a href="../w3cnote/python-docstrings.html" target="_blank" rel="noopener nofollow">Python 文档字符串(DocStrings)</a></p></blockquote>

<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">Mr.Wu</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Mr.Wu</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;928***263@qq.com</p></div></span>5年前 (2018-07-20)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-29889"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Mr.Wu</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;928***263@qq.com</p></div><div id="runoobvote-id-29889" data-commid = "29889" class="upvotejs"><a class="upvote"></a> <span class="count">90</span></div></div><div class="comt-main" id="div-comment-29889"><p data-title="Python 函数返回多个值">函数返回值的注意事项: 不同于 C 语言，Python 函数可以返回多个值，多个值以元组的方式返回:</p>

<pre>
def fun(a,b):    
    "返回多个值，结果以元组形式表示"
    return a,b,a+b
print(fun(1,2))
</pre>
<p>输出结果为：</p>

<pre>(1, 2, 3)</pre>
<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="../note/29889">Mr.Wu</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Mr.Wu</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;928***263@qq.com</p></div></span>5年前 (2018-07-20)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-30597"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;sprinkle</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;117***1554@qq.com</p></div><div id="runoobvote-id-30597" data-commid = "30597" class="upvotejs"><a class="upvote"></a> <span class="count">40</span></div></div><div class="comt-main" id="div-comment-30597"><p><strong>函数的装饰器</strong></p>

<p>在不改变当前函数的情况下, 给其增加新的功能:</p>

<pre>def log(pr):#将被装饰函数传入
    def wrapper():
        print("**********")      
        return pr()#执行被装饰的函数
    return wrapper#将装饰完之后的函数返回（返回的是函数名）
@log
def pr():
    print("我是小小洋")

pr()</pre>
<p>回调函数和返回函数的实例就是装饰器。</p>


<blockquote><p>更多内容可参考：<a href="../w3cnote/python-func-decorators.html" target="_blank" rel="noopener nofollow">Python 函数装饰器</a></p></blockquote>

<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">sprinkle</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;sprinkle</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;117***1554@qq.com</p></div></span>5年前 (2018-07-28)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-34820"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Null</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;wuh***ing2012@163.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="../w3cnote/python-unboundlocalerror.html">&nbsp;&nbsp;参考地址</a></p></div><div id="runoobvote-id-34820" data-commid = "34820" class="upvotejs"><a class="upvote"></a> <span class="count">26</span></div></div><div class="comt-main" id="div-comment-34820"><p data-title="Python 内部函数">1.内部函数，不修改全局变量可以访问全局变量</p>

<pre>a = 10
def test():
    b = a + 2 #仅仅访问全局变量 a
    print(b)
test()</pre>
<p>输出结果为：</p>

<pre>12</pre>
<p>2.内部函数，修改同名全局变量，则python会认为它是一个局部变量（同教程最后一个例子）</p>

<pre>#!/usr/bin/python3
 
a = 10
def test():
    a = a + 1 #修改同名的全局变量，则认为是一个局部变量
    print(a)
test()</pre>
<p>3.在内部函数修改同名全局变量之前调用变量名称（如print sum），则引发Unbound-LocalError</p>

<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="../note/34820">Null</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;Null</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;wuh***ing2012@163.com</p><p><i class="fa fa-external-link" aria-hidden="true"></i> <a rel="nofollow" target="_blank" href="../w3cnote/python-unboundlocalerror.html">&nbsp;&nbsp;参考地址</a></p></div></span>5年前 (2018-09-06)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-37802"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;richael</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;ric***l0302@163.com</p></div><div id="runoobvote-id-37802" data-commid = "37802" class="upvotejs"><a class="upvote"></a> <span class="count">17</span></div></div><div class="comt-main" id="div-comment-37802"><p>在这里补充一点关于 global 和 nonlocal 的知识：</p>

<p>nonlocal 只能修改外层函数的变量而不能修改外层函数所引用的全局变量，给一个例子如下：</p>

<pre>x = 0
def outer():
    global x
    x = 1    
    def inner():
        nonlocal x
        x = 2        
        print(x)
    inner()

outer()
print(x)</pre>
<p>结果会报错：</p>

<pre>line 6
    nonlocal x
    ^
SyntaxError: no binding for nonlocal 'x' found</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">richael</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;richael</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;ric***l0302@163.com</p></div></span>4年前 (2018-12-06)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-39046"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;爱睡觉的猫</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;199***09662@163.com</p></div><div id="runoobvote-id-39046" data-commid = "39046" class="upvotejs"><a class="upvote"></a> <span class="count">52</span></div></div><div class="comt-main" id="div-comment-39046"><p><strong>global</strong> 关键字会跳过中间层直接将嵌套作用域内的局部变量变为全局变量:</p>

<p>测试代码如下:</p>

<pre>num = 20
def outer():
    num = 10
    def inner():
        global num
        print (num)
        num = 100
        print (num)
    inner()
    print(num)
outer()
print (num)</pre>
    
结果如下：

<pre>20
100
10
100</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">爱睡觉的猫</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;爱睡觉的猫</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;199***09662@163.com</p></div></span>4年前 (2019-01-16)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-40479"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;濤聲依舊</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;152***4927@qq.com</p></div><div id="runoobvote-id-40479" data-commid = "40479" class="upvotejs"><a class="upvote"></a> <span class="count">19</span></div></div><div class="comt-main" id="div-comment-40479"><p>Python 定义一个 class 可以编写一个它的构造函数 _init_() 其中形参 self 在实例化时相当于 myname：</p>

<pre>class demo:
    name = ""
    def _init_(self):
        self.ex()
        self.start()
    def inputName(self):
        global name
        name = input("輸入您的姓名：")
    def getFirstName(self):
        if len(name) &lt;= 0:
            x = "別鬧！請輸入姓名！"
            return x
        else:
            x = name[0]
            return x
    def getLastName(self):
        if len(name) &lt;= 1:
            y = "別鬧！長度不夠！"
            return y
        else:
            y = name[1:]
            return y
myname = demo()
myname.inputName()
print(myname.getFirstName())
print(myname.getLastName())</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">濤聲依舊</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;濤聲依舊</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;152***4927@qq.com</p></div></span>4年前 (2019-03-07)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-43035"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;fanchen</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;227***223@qq.com</p></div><div id="runoobvote-id-43035" data-commid = "43035" class="upvotejs"><a class="upvote"></a> <span class="count">32</span></div></div><div class="comt-main" id="div-comment-43035"><p>函数的参数分为<strong>形参</strong>和<strong>实参</strong>。</p>

<p><strong>1. 什么是形参</strong></p>
<p>对于函数来说，形式参数简称形参，是指在定义函数时，定义的一个变量名。</p>
<p>下面的代码中，x、y、z 就是形参。</p>

<pre>#定义函数

def foo(x, y, z):
      print("x=", x)
      print("y=", y)
      print("z=", z)

#调用函数
foo(1,3,5)                #此处的1,3,5是实参</pre>

<p>输出结果：</p>
<pre>x= 1
y= 3
z= 5</pre>

<p>形参的作用：是用来接收调用函数时输入的值。</p>

<p><strong>2. 什么是实参</strong></p>

<p>对于函数来说，实际参数简称实参。</p>

<p>是指在调用函数时传入的实际的数据，这会被绑定到函数的形参上。</p>

<p>函数调用时，将值绑定到变量名上，函数调用结束，解除绑定，并且实参将不再存在于程序中。</p>

<pre>foo(5,2,0)</pre>

<p>上面的 5、 2 和 0 都是实参。</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">fanchen</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;fanchen</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;227***223@qq.com</p></div></span>4年前 (2019-05-23)</div></div></li><!-- #comment-## -->
<li class="comment byuser comment-author-tianqixin bypostauthor odd alt thread-odd thread-alt depth-1" id="comment-47123"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;べ断桥烟雨ミ</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;Leg***_1949@126.com</p></div><div id="runoobvote-id-47123" data-commid = "47123" class="upvotejs"><a class="upvote"></a> <span class="count">67</span></div></div><div class="comt-main" id="div-comment-47123"><p>在编写函数的过程中，可以显式指定函数的参数类型及返回值类型：</p>

<pre>#!/usr/bin/env python3
# -*- coding: UTF-8 -*-

def function_demo(param_A: int, param_B: float, param_C: list, param_D: tuple) -&gt; dict:
    pass</pre>
<p>这种 <strong>“将数据类型写死在代码中”</strong> 的行为在集成开发环境/代码编辑器时尤为方便，通过显式地指定函数的参数类型和返回值，能够让智能补全组件提前获知标识符的数据类型，提供有利的辅助开发功能。</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">べ断桥烟雨ミ</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;べ断桥烟雨ミ</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;Leg***_1949@126.com</p></div></span>4年前 (2019-09-29)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-50273"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;李宇</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;146***0060@qq.com</p></div><div id="runoobvote-id-50273" data-commid = "50273" class="upvotejs"><a class="upvote"></a> <span class="count">26</span></div></div><div class="comt-main" id="div-comment-50273"><p>对于上面提到的向函数中传递函数对象的用法，我这里进一步补充。</p>

<p><strong>1.向函数传递的函数本身可以是有参数的:</strong></p>

<pre>def demo(*p):
    i=0
    for var in p:
        var(i)
        i+=1
def d1(i):
    print("这里是第一个子函数,输入的数是",i)
def d2(i):
    print("这里是第二个子函数,输入的数是",i)
def d3(i):
    print("这里是第三个子函数,输入的数是",i)
def d4(i):
    print("这里是第四个子函数,输入的数是",i)
demo(d1,d2,d3,d4)</pre>
<p>上面的代码执行起来没问题。</p>

<p><strong>2.就算你的函数是有参数的，将这个函数传递给另一个函数的时候也不能加参数，还是上面的例子:</strong></p>

<pre>demo(d1(7),d2,d3,d4)</pre><p>
这样就会报错，因为此时 d1(7) 就是 d1() 的返回值，是不可以在方法内部传递参数并且调用。</p><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">李宇</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;李宇</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;146***0060@qq.com</p></div></span>3年前 (2020-01-24)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-59542"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;zppet</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;zpp***sina.com</p></div><div id="runoobvote-id-59542" data-commid = "59542" class="upvotejs"><a class="upvote"></a> <span class="count">16</span></div></div><div class="comt-main" id="div-comment-59542"><p>关于函数参数能不能在函数里被改变。</p>
<p>我自己的理解是：如果函数内部出现过赋值语句，则意味着新的对象被生成，函数入参所指向的对象就改变了。这时，再改变，也只是操作的新生成的对象。</p>
<p>测试代码如下：</p>

<pre>#!/usr/bin/python3
# -*-coding:utf-8 -*-
# 可写函数说明
def changeme( mylist ):
   "修改传入的列表"
   b=mylist
   print(id(b))
   print(id(mylist))
   mylist[1]="Hello World!"
   print ("函数内取值1: ", mylist)
   print("b:",b)
   mylist.append([1,2,3,4])
   #mylist=[1,2,3,4]   
   print ("函数内取值2: ", mylist,b)
   mylist=[11,21,31,41]   
   print ("函数内取值3: ", mylist,b)
   return
 
# 调用changeme函数
mylist = [10,20,30]
changeme( mylist )
print ("函数外取值: ", mylist)</pre>
<p>输出结果：</p>

<pre>55061120
55061120
函数内取值1:  [10, 'Hello World!', 30]
b: [10, 'Hello World!', 30]
函数内取值2:  [10, 'Hello World!', 30, [1, 2, 3, 4]] [10, 'Hello World!', 30, [1, 2, 3, 4]]
函数内取值3:  [11, 21, 31, 41] [10, 'Hello World!', 30, [1, 2, 3, 4]]
函数外取值:  [10, 'Hello World!', 30, [1, 2, 3, 4]]</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">zppet</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;zppet</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;zpp***sina.com</p></div></span>2年前 (2020-12-15)</div></div></li><!-- #comment-## -->
<li class="comment even thread-even depth-1" id="comment-59975"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;shs1992shs</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;457***946@qq.com</p></div><div id="runoobvote-id-59975" data-commid = "59975" class="upvotejs"><a class="upvote"></a> <span class="count">11</span></div></div><div class="comt-main" id="div-comment-59975"><p>针对楼上 <a href="#comment-22272" target="_blank" rel="noopener nofollow ugc">@IS</a> 的关于函数内部可以访问全局变量，但是不能更新（修改）其值。经过测试，其实函数内部是可以修改全局变量的，测试代码如下：</p>

<p>1、访问全局变量：</p>


<pre>a = 10
def sum ( n ) :
    n += a    # 调用全局变量a
    print ('a = ', a, end = ' , ' )
    print ( 'n = ', n )

sum(3)</pre>


<p>输出 :</p>

<pre>a =  10 , n = 13</pre>

<p>2、在函数内部重新赋值全局变量，再次调用：</p>


<pre>a = 10
def sum ( n ) :
    a = 20  # 重新赋值
    n += a # 调用全局变量a
    print ('a = ', a, end = ' , ' )
    print ( 'n = ', n )

sum(3)</pre>


<p>输出 :</p>

<pre>a=  20 , n =  23</pre>
<p>
3、如果函数内部调用了全局变量，但之后又修改了该变量，则会报错（引发 UnboundLocalError） :</p>


<pre>a = 10
def sum ( n ) :
   n += a  # 调用了全局变量a，但是之后又修改了a，引发错误，若将a=11注释掉则不会报错，或将a=11语句置于调用语句之前也不会报错
   a = 11  # 修改了全局变量
   print ('a = ', a, end = ' , ' )
   print ( 'n = ', n )

sum(3)</pre>


<p>输出 :</p>


<pre>...
UnboundLocalError:local variable 'a' referenced before assignment</pre>

<p>
4、如果函数内部调用了全局变量之前已经重新赋值了该全局变量，则在调用之后，再次修改其值，也不会报错</p>


<pre>a = 10
def sum ( n ) :
   a = 20  # 修改了全局变量
   n += a   # 调用了全局变量a
   a = 11  # 再次修改全局变量
   print ('a = ', a, end = ' , ' )
   print ( 'n = ', n )

sum(3)</pre>
<p>输出 :</p>

<pre>a=  11 , n =  23</pre>
<p>
5、可以加上 global 引用以更新变量值:</p>


<pre>a = 10
def sum ( n ) :
   global a
   n += a
   a = 11
   print ('a = ', a, end = ' , ' )
   print ( 'n = ', n )

sum ( 3 )
print( '外a = ', a )</pre>


<p>输出: </p>

<pre>a =11 , n = 13 外 a = 11</pre><div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">shs1992shs</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;shs1992shs</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;457***946@qq.com</p></div></span>2年前 (2020-12-29)</div></div></li><!-- #comment-## -->
<li class="comment odd alt thread-odd thread-alt depth-1" id="comment-62415"><span class="comt-f">#0</span><div class="comt-avatar wrapper"><i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i><div class="tooltip"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;codeduck1</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;cod***ck@aliyun.com</p></div><div id="runoobvote-id-62415" data-commid = "62415" class="upvotejs"><a class="upvote"></a> <span class="count">152</span></div></div><div class="comt-main" id="div-comment-62415"><p>建议楼上搞懂什么是全局变量和局部变量再来写评论，简直是误人子弟。</p>

<pre>a = 10  # 此处a是声明全部变量
def sum ( n ) :
    a = 20  # 此处a是声明局部变量，局部变量会随着方法出栈而消失(闭包除外)
    n += a  # 此处调用的是局部变量a
    print ('a = ', a, end = ' , ' )
    print ( 'n = ', n )
a = 10
def sum ( n ) :
   n += a  # 此处引用全局变量a, 注意：这个函数内并没有声明局部变量a
   a = 11  # 此处修改全局变量a, 必然会报错！如要函数内修改全部变量，用global声明就不会报错
   print ('a = ', a, end = ' , ' )
   print ( 'n = ', n )</pre>
<p>楼上下面的函数也是，不一 一描述。。。</p>

<div class="comt-meta wrapper"><span class="comt-author"><a target="_blank" href="javascript:;">codeduck1</a><div class="tooltip tooltip1"><p><i class="fa fa-user" aria-hidden="true"></i>&nbsp;&nbsp;&nbsp;codeduck1</p><p><i class="fa fa-envelope" aria-hidden="true"></i>&nbsp;&nbsp;cod***ck@aliyun.com</p></div></span>2年前 (2021-03-23)</div></div></li><!-- #comment-## -->
	</ol>
	<div class="pagenav">
			</div>
</div>
<div id="respond" class="no_webshot"> 
		<div class="comment-signarea" style="display:none; padding: 20px 20px;"> 
	<h3 class="text-muted" id="share_code" style="color: #799961;"><i class="fa fa-pencil-square-o" aria-hidden="true"></i> 点我分享笔记</h3>
	<!--
	<p style="font-size:14px;">笔记需要是本篇文章的内容扩展！</p><br>
	<p style="font-size:12px;"><a href="../tougao" target="_blank">文章投稿，可点击这里</a></p>
	<p style="font-size:14px;"><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>
		<h3 class="text-muted"><i class="fa fa-info-circle" aria-hidden="true"></i> 分享笔记前必须<a href="javascript:;" class="runoob-pop">登录</a>！</h3>
		<p><a href="../w3cnote/runoob-user-test-intro.html#invite" target="_blank">注册邀请码获取方式</a></p>-->
	</div>
		
	<form action="/wp-content/themes/runoob/option/addnote.php" method="post" id="commentform" style="display:none;">
		<div class="comt">
			<div class="comt-title">
				<i style="font-size:36px;" class="fa fa-user-circle" aria-hidden="true"></i>				<p><a id="cancel-comment-reply-link" href="javascript:;">取消</a></p>
			</div>
			<div class="comt-box">
			<div id="mded"></div>
			
				<div class="comt-ctrl">
					<div class="comt-tips"><input type='hidden' name='comment_post_ID' value='7303' id='comment_post_ID' />
<input type='hidden' name='comment_parent' id='comment_parent' value='0' />
</div>
					<button type="submit" name="submit" id="submit" tabindex="5"><i class="fa fa-pencil" aria-hidden="true"></i> 分享笔记</button>
				</div>
			</div>
		
				
					<div class="comt-comterinfo"> 
						<ul id="comment-author-info">
							<li class="form-inline"><label class="hide" for="author">昵称</label><input class="ipt" type="text" name="author" id="author" value="" tabindex="2" placeholder="昵称"><span class="text-muted">昵称 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="email">邮箱</label><input class="ipt" type="text" name="email" id="email" value="" tabindex="3" placeholder="邮箱"><span class="text-muted">邮箱 (必填)</span></li>
							<li class="form-inline"><label class="hide" for="url">引用地址</label><input class="ipt" type="text" name="url" id="url" value="" tabindex="4" placeholder="引用地址"><span class="text-muted">引用地址</span></li>
						</ul>
					</div>
				
			
		</div>

	</form>
	</div>
<script type="text/javascript">
$(function() {
	//初始化编辑器
	
	var editor = new Simditor({
	  textarea: $('#mded'),
	  placeholder: '写笔记...',
	  upload:false,
	 // upload: {url:'/api/comment_upload_file.php',params: null,fileKey: 'upload_file',connectionCount: 1,leaveConfirm: '文件正在上传，您确定离开?'},
	  defaultImage: 'https://www.runoob.com/images/logo.png',
	  codeLanguages: '',
	  autosave: 'editor-content',
	  toolbar: [  'bold','code','ul','ol','image' ]
	});
	editor.on('selectionchanged', function() {
		$(".code-popover").hide();
	});

	// 提交数据
	$("#share_code").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		
	});
	$("#user_add_note").click(function() {
		$(".comment-signarea").hide();
		$("#commentform").show();
		$('html, body').animate({
       	    scrollTop: $("#respond").offset().top
    	}, 200);
	});

	// 提交笔记
	var commentform=$('#commentform');
	commentform.prepend('<div id="comment-status" style="display:none;" ></div>');
	var statusdiv=$('#comment-status');
	
	commentform.submit(function(e){
		e.preventDefault();
		var noteContent = editor.getValue();
		// console.log(noteContent);
		noteContent = noteContent.replace(/<pre><code>/g,"<pre>");
		noteContent = noteContent.replace(/<\/code><\/pre>/g,"</pre>");
		
		// 系列化表单数据
		var comment_parent = 0;
		var is_user_logged_in = $("#is_user_logged_in").val();
		var comment_post_ID =  7303;
		var _wp_unfiltered_html_comment = $("#_wp_unfiltered_html_comment").val();
		var comment = noteContent;
		var author = $("#author").val();
		var url = $("#url").val();
		var email = $("#email").val();
		if(isBlank(author) && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入昵称！</p>').show();
		} else if(isBlank(email)  && is_user_logged_in==0) {
			statusdiv.html('<p  class="ajax-error">请输入邮箱！</p>').show();
		} else {
			// var formdata=commentform.serialize() + "&comment=" + noteContent ;
			// 添加状态信息
			statusdiv.html('<p>Processing...</p>').show();
			// 获取表单提交地址
			var formurl=commentform.attr('action');
			
			// 异步提交
			$.ajax({
					type: 'post',
					url: formurl,
					dataType:'json',
					data: {"comment_parent":comment_parent,"comment_post_ID":comment_post_ID, "_wp_unfiltered_html_comment":_wp_unfiltered_html_comment,"comment":comment,"url":url, "email":email,"author":author},
					error: function(XMLHttpRequest, textStatus, errorThrown){
					statusdiv.html('<p class="ajax-error" >数据不完整或表单提交太快了！</p>').show();
				},
				success: function(data, textStatus){
					if(data.errorno=="0") {
						$("#submit").prop('disabled', true);
						statusdiv.html('<p class="ajax-success" >笔记已提交审核，感谢分享笔记！</p>').show();
						alert('笔记已提交审核，感谢分享笔记！');
					}else{
						statusdiv.html('<p class="ajax-error" >'+data.msg+'</p>').show();
					}
					commentform.find('textarea[name=comment]').val('');
				}
			});
			setTimeout(function(){
		        $("#submit").prop('disabled', false);
		    }, 10*1000);
		}
		return false;

	});
	$(".comt-author").click(function() {
		href = $(this).children("a").attr("href");
		if(href.indexOf("/note/")!=-1) {
			var win = window.open(href, '_blank');
  			win.focus();
		}
	});
	$(".comt-meta span").hover(function(){
		$(this).children(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).children(".tooltip").removeAttr("style");
	});
	/*
	$(".wrapper i").hover(function(){
		$(this).siblings(".tooltip").css({ "opacity": 1, "pointer-events": "auto"});
	},function(){
		$(this).siblings(".tooltip").css({ "opacity": 0, "pointer-events": "auto"});
	});
	*/
	//Upvote.create('runoobvote-id', {callback: vote_callback});
	var ajaxurl = 'https://www.runoob.com/wp-admin/admin-ajax.php';
	var callback = function(data) {
		//console.log($('#runoobvote-id').upvote('upvoted'));
		//console.log($('#runoobvote-id').upvote('downvoted'));
		//console.log(data);
		_vote_action = data.action;
		id_arr = data.id.split('-');
		um_id= id_arr[2];
		//console.log(um_id);
		
		var re = /^[1-9]+/;
		if (re.test(um_id)) { 
			var ajax_data = {
				_vote_action: _vote_action,
				action: "pinglun_zan",
				um_id: um_id,
				um_action: "ding"
			};
			//console.log(ajax_data);
			$.post(ajaxurl,ajax_data,function(status){
				//if(status.vote_num>999) {
				//	_voteHtml = '<span style="display: block; text-align: center;font-size: 20px; color: #6a737c; margin: 8px 0;">'+kFormatter(status.vote_num) +'</span>';
				//	$("#runoobvote-id-" + um_id + " .count").hide().after(_voteHtml);
				//}
				
			});
		}
	};
	if($('#comments').length && $('.upvotejs').length){
		$('.upvotejs').upvote({id: 7303, callback: callback});
	
		$.post(ajaxurl,{"action":"pinglun_zan","postid":7303},function(data){  
			$(data).each(function(key,value) {
				$("#runoobvote-id-" + value.commid + " .upvote").addClass(value.upvotejs_class);
				$("#runoobvote-id-" + value.commid + " .downvote").addClass(value.downvote_class);
				$("#runoobvote-id-" + value.commid + " .count").text(value.upvote_count);
			})
		},'json');
		
	}
	
	
});
function isBlank(str) {
    return (!str || /^\s*$/.test(str));
}
function kFormatter(num) {
	// return num;
    return Math.abs(num) > 999 ? Math.sign(num)*((Math.abs(num)/1000).toFixed(1)) + 'k' : Math.sign(num)*Math.abs(num)
}

</script>

<link rel="stylesheet" href="../0/qa.css?1.44">
<link rel="stylesheet" type="text/css" href="../0/simditor.min.css" />
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
<script type="text/javascript" src=""></script>
		<div class="sidebar-box ">
				

<style>	
.responsive_ad1 { display:none; }
@media(min-width: 800px) { .responsive_ad1 {  display:block;margin:0 auto;} }
</style>
<div id="ad-336280" >

<script async src=""
     crossorigin="anonymous"></script>
<!-- 移动版 自动调整 -->
<ins class="adsbygoogle"
     style="display:block"
     data-ad-client="ca-pub-5751451760833794"
     data-ad-slot="1691338467"
     data-ad-format="auto"
     data-full-width-responsive="true"></ins>
<script>
     (adsbygoogle = window.adsbygoogle || []).push({});
</script>
		</div>
				</div>
		
	</div>
</div>
	

<!-- 右边栏 -->
<div class="fivecol last right-column">

<style>
.sidebar-tree .double-li {
    width:300px;
}
.sidebar-tree .double-li li {
    width: 44%;
    line-height: 1.5em;
    border-bottom: 1px solid #ccc;
    float: left;
    display: inline;
}
</style>

    
        <div class="sidebar-box re-box re-box-large">
        <div class="sidebar-box recommend-here" style="margin: 0 auto;">
            <a href="javascript:void(0);" style="font-size: 16px; color:#64854c;font-weight:bold;"> <i class="fa fa-list" aria-hidden="true"></i> 分类导航</a>
        </div>
    <div class="sidebar-box sidebar-cate">
        
        <div class="sidebar-tree" >
            <ul><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> HTML / CSS</a><ul class="double-li"><li><a title="HTML 教程" href="../html/html-tutorial.html">HTML 教程</a></li><li><a title="HTML5 教程" href="../html/html5-intro.html">HTML5 教程</a></li><li><a title="CSS 教程" href="../css/css-tutorial.html">CSS 教程</a></li><li><a title="CSS3 教程" href="../css3/css3-tutorial.html">CSS3 教程</a></li><li><a title="Bootstrap3 教程" href="../bootstrap/bootstrap-tutorial.html">Bootstrap3 教程</a></li><li><a title="Bootstrap4 教程" href="../bootstrap4/bootstrap4-tutorial.html">Bootstrap4 教程</a></li><li><a title="Bootstrap5 教程" href="../bootstrap5/bootstrap5-tutorial.html">Bootstrap5 教程</a></li><li><a title="Font Awesome 教程" href="../font-awesome/fontawesome-tutorial.html">Font Awesome 教程</a></li><li><a title="Foundation 教程" href="../foundation/foundation-tutorial.html">Foundation 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> JavaScript</a><ul class="double-li"><li><a title="JavaScript 教程" href="../js/js-tutorial.html">JavaScript 教程</a></li><li><a title="HTML DOM 教程" href="../htmldom/htmldom-tutorial.html">HTML DOM 教程</a></li><li><a title="jQuery 教程" href="../jquery/jquery-tutorial.html">jQuery 教程</a></li><li><a title="AngularJS 教程" href="../angularjs/angularjs-tutorial.html">AngularJS 教程</a></li><li><a title="AngularJS2 教程" href="../angularjs2/angularjs2-tutorial.html">AngularJS2 教程</a></li><li><a title="Vue.js 教程" href="../vue2/vue-tutorial.html">Vue.js 教程</a></li><li><a title="Vue3 教程" href="../vue3/vue3-tutorial.html">Vue3 教程</a></li><li><a title="React 教程" href="../react/react-tutorial.html">React 教程</a></li><li><a title="TypeScript 教程" href="../typescript/ts-tutorial.html">TypeScript 教程</a></li><li><a title="jQuery UI 教程" href="../jqueryui/jqueryui-tutorial.html">jQuery UI 教程</a></li><li><a title="jQuery EasyUI 教程" href="../jeasyui/jqueryeasyui-tutorial.html">jQuery EasyUI 教程</a></li><li><a title="Node.js 教程" href="../nodejs/nodejs-tutorial.html">Node.js 教程</a></li><li><a title="AJAX 教程" href="../ajax/ajax-tutorial.html">AJAX 教程</a></li><li><a title="JSON 教程" href="../json/json-tutorial.html">JSON 教程</a></li><li><a title="Echarts 教程" href="../echarts/echarts-tutorial.html">Echarts 教程</a></li><li><a title="Chart.js 教程" href="../chartjs/chartjs-tutorial.html">Chart.js 教程</a></li><li><a title="Highcharts 教程" href="../highcharts/highcharts-tutorial.html">Highcharts 教程</a></li><li><a title="Google 地图 教程" href="../googleapi/google-maps-basic.html">Google 地图 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 服务端</a><ul class="double-li"><li><a title="Python 教程" href="../python3/python3-tutorial.html">Python 教程</a></li><li><a title="Python2.x 教程" href="../python/python-tutorial.html">Python2.x 教程</a></li><li><a title="Linux 教程" href="../linux/linux-tutorial.html">Linux 教程</a></li><li><a title="Docker 教程" href="../docker/docker-tutorial.html">Docker 教程</a></li><li><a title="Ruby 教程" href="../ruby/ruby-tutorial.html">Ruby 教程</a></li><li><a title="Java 教程" href="../java/java-tutorial.html">Java 教程</a></li><li><a title="C 教程" href="../c/c-tutorial.html">C 教程</a></li><li><a title="C++ 教程" href="../cplusplus/cpp-tutorial.html">C++ 教程</a></li><li><a title="Perl 教程" href="../perl/perl-tutorial.html">Perl 教程</a></li><li><a title="Servlet 教程" href="../servlet/servlet-tutorial.html">Servlet 教程</a></li><li><a title="JSP 教程" href="../jsp/jsp-tutorial.html">JSP 教程</a></li><li><a title="Lua 教程" href="../lua/lua-tutorial.html">Lua 教程</a></li><li><a title="Rust 教程" href="../rust/rust-tutorial.html">Rust 教程</a></li><li><a title="Scala 教程" href="../scala/scala-tutorial.html">Scala 教程</a></li><li><a title="Go 教程" href="../go/go-tutorial.html">Go 教程</a></li><li><a title="PHP 教程" href="../php/php-tutorial.html">PHP 教程</a></li><li><a title="数据结构与算法" href="../data-structures/data-structures-tutorial.html">数据结构与算法</a></li><li><a title="Django 教程" href="../django/django-tutorial.html">Django 教程</a></li><li><a title="Zookeeper 教程" href="../w3cnote/zookeeper-tutorial.html">Zookeeper 教程</a></li><li><a title="设计模式" href="../design-pattern/design-pattern-tutorial.html">设计模式</a></li><li><a title="正则表达式" href="../regexp/regexp-tutorial.html">正则表达式</a></li><li><a title="Maven 教程" href="../maven/maven-tutorial.html">Maven 教程</a></li><li><a title="Verilog 教程" href="../w3cnote/verilog-tutorial.html">Verilog 教程</a></li><li><a title="ASP 教程" href="../asp/asp-tutorial.html">ASP 教程</a></li><li><a title="AppML 教程" href="../appml/appml-tutorial.html">AppML 教程</a></li><li><a title="VBScript 教程" href="../vbscript/vbscript-tutorial.html">VBScript 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 数据库</a><ul class="double-li"><li><a title="SQL 教程" href="../sql/sql-tutorial.html">SQL 教程</a></li><li><a title="MySQL 教程" href="../mysql/mysql-tutorial.html">MySQL 教程</a></li><li><a title="PostgreSQL 教程" href="../postgresql/postgresql-tutorial.html">PostgreSQL 教程</a></li><li><a title="SQLite 教程" href="../sqlite/sqlite-tutorial.html">SQLite 教程</a></li><li><a title="MongoDB 教程" href="../mongodb/mongodb-tutorial.html">MongoDB 教程</a></li><li><a title="Redis 教程" href="../redis/redis-tutorial.html">Redis 教程</a></li><li><a title="Memcached 教程" href="../Memcached/Memcached-tutorial.html">Memcached 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 数据分析</a><ul class="double-li"><li><a title="Python 教程" href="../python3/python3-tutorial.html">Python 教程</a></li><li><a title="NumPy 教程" href="../numpy/numpy-tutorial.html">NumPy 教程</a></li><li><a title="Pandas 教程" href="../pandas/pandas-tutorial.html">Pandas 教程</a></li><li><a title="Matplotlib 教程" href="../matplotlib/matplotlib-tutorial.html">Matplotlib 教程</a></li><li><a title="Scipy 教程" href="../scipy/scipy-tutorial.html">Scipy 教程</a></li><li><a title="R 教程" href="../r/r-tutorial.html">R 教程</a></li><li><a title="Julia 教程" href="../julia/julia-tutorial.html">Julia 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 移动端</a><ul class="double-li"><li><a title="Android 教程" href="../w3cnote/android-tutorial-intro.html">Android 教程</a></li><li><a title="Swift 教程" href="../swift/swift-tutorial.html">Swift 教程</a></li><li><a title="jQuery Mobile 教程" href="../jquerymobile/jquerymobile-tutorial.html">jQuery Mobile 教程</a></li><li><a title="ionic 教程" href="../ionic/ionic-tutorial.html">ionic 教程</a></li><li><a title="Kotlin 教程" href="../kotlin/kotlin-tutorial.html">Kotlin 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> XML 教程</a><ul class="double-li"><li><a title="XML 教程" href="../xml/xml-tutorial.html">XML 教程</a></li><li><a title="DTD 教程" href="../dtd/dtd-tutorial.html">DTD 教程</a></li><li><a title="XML DOM 教程" href="../dom/dom-tutorial.html">XML DOM 教程</a></li><li><a title="XSLT 教程" href="../xsl/xsl-tutorial.html">XSLT 教程</a></li><li><a title="XPath 教程" href="../xpath/xpath-tutorial.html">XPath 教程</a></li><li><a title="XQuery 教程" href="../xquery/xquery-tutorial.html">XQuery 教程</a></li><li><a title="XLink 教程" href="../xlink/xlink-tutorial.html">XLink 教程</a></li><li><a title="XPointer 教程" href="../xlink/xlink-tutorial.html">XPointer 教程</a></li><li><a title="XML Schema 教程" href="../schema/schema-tutorial.html">XML Schema 教程</a></li><li><a title="XSL-FO 教程" href="../xslfo/xslfo-tutorial.html">XSL-FO 教程</a></li><li><a title="SVG 教程" href="../svg/svg-tutorial.html">SVG 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> ASP.NET</a><ul class="double-li"><li><a title="ASP.NET 教程" href="../aspnet/aspnet-tutorial.html">ASP.NET 教程</a></li><li><a title="C# 教程" href="../csharp/csharp-tutorial.html">C# 教程</a></li><li><a title="Web Pages 教程" href="../aspnet/webpages-intro.html">Web Pages 教程</a></li><li><a title="Razor 教程" href="../aspnet/razor-intro.html">Razor 教程</a></li><li><a title="MVC 教程" href="../aspnet/mvc-intro.html">MVC 教程</a></li><li><a title="Web Forms 教程" href="../aspnet/aspnet-intro.html">Web Forms 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> Web Service</a><ul class="double-li"><li><a title="Web Service 教程" href="../webservices/webservices-tutorial.html">Web Service 教程</a></li><li><a title="WSDL 教程" href="../wsdl/wsdl-tutorial.html">WSDL 教程</a></li><li><a title="SOAP 教程" href="../soap/soap-tutorial.html">SOAP 教程</a></li><li><a title="RSS 教程" href="../rss/rss-tutorial.html">RSS 教程</a></li><li><a title="RDF 教程" href="../rdf/rdf-tutorial.html">RDF 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 开发工具</a><ul class="double-li"><li><a title="Eclipse 教程" href="../eclipse/eclipse-tutorial.html">Eclipse 教程</a></li><li><a title="Git 教程" href="../git/git-tutorial.html">Git 教程</a></li><li><a title="Svn 教程" href="../svn/svn-tutorial.html">Svn 教程</a></li><li><a title="Markdown 教程" href="../markdown/md-tutorial.html">Markdown 教程</a></li></ul></li><li style="margin: 0;"><a href="javascript:void(0);" class="tit"> 网站建设</a><ul class="double-li"><li><a title="HTTP 教程" href="../http/http-tutorial.html">HTTP 教程</a></li><li><a title="网站建设指南" href="../web/web-buildingprimer.html">网站建设指南</a></li><li><a title="浏览器信息" href="../browsers/browser-information.html">浏览器信息</a></li><li><a title="网站主机教程" href="../hosting/hosting-tutorial.html">网站主机教程</a></li><li><a title="TCP/IP 教程" href="../tcpip/tcpip-tutorial.html">TCP/IP 教程</a></li><li><a title="W3C 教程" href="../w3c/w3c-tutorial.html">W3C 教程</a></li><li><a title="网站品质" href="../quality/quality-tutorial.html">网站品质</a></li></ul></li></ul>            </div>
    
    </div>
    </div>
    <br>
    
    <div class="sidebar-box re-box re-box-large">
        <div class="sidebar-box recommend-here">
            <a href="javascript:void(0);">Advertisement</a>
        </div>
        <div class="re-600160" id="sidebar-right-re">
                <script async src=""></script>
        <!-- 侧栏1 -->
        <ins class="adsbygoogle"
             style="display:inline-block;width:160px;height:600px"
             data-ad-client="ca-pub-5751451760833794"
             data-ad-slot="4106274865"></ins>
        <script>
        (adsbygoogle = window.adsbygoogle || []).push({});
        </script>
                </div>
    </div>
</div></div>

</div>

<script>
var aid = 7303;
function coll() {
	$.post( '/wp-content/themes/runoob/option/user/userinfo.php', {aid:aid, action:"collarticle", opt:'add'},function( data ) {
		if(data.error==0) {
			$("#content").find("h1:first").find("a").attr("href","javascript:void(0);");
			$("#content").find("h1:first").find("img").attr("src","http://www.runoob.com/wp-content/themes/runoob/assets/img/coll2.png").css({width:32+"px",height:32+"px"});
		}
		alert(data.msg);
	},'json');
}
</script>


<!-- 反馈对话框开始 -->
<script src=""></script>
<link rel="stylesheet" href="../0/feedback.css?1.0" />
<script type="text/javascript">
$.feedback({
    ajaxURL: '/feedback/runoob_feedback.php',
	html2canvasURL: '/wp-content/themes/runoob/assets/feedback/stable/2.0/html2canvas.js',
	onClose: function () {
         window.location.reload();
    }
});
</script>
<!-- 反馈对话框结束 -->
<button class="feedback-btn feedback-btn-gray">反馈/建议</button>
<!-- 底部 -->
<div id="footer" class="mar-t50">
   <div class="runoob-block">
    <div class="runoob cf">
     <dl>
      <dt>
       在线实例
      </dt>
      <dd>
       &middot;<a target="_blank" href="../html/html-examples.html">HTML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../css/css-examples.html">CSS 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../js/js-examples.html">JavaScript 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../ajx/ajax-examples.html">Ajax 实例</a>
      </dd>
       <dd>
       &middot;<a target="_blank" href="../jquery/jquery-examples.html">jQuery 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../xml/xml-examples.html">XML 实例</a>
      </dd>
      <dd>
       &middot;<a target="_blank" href="../java/java-examples.html">Java 实例</a>
      </dd>
     
     </dl>
     <dl>
      <dt>
      字符集&工具
      </dt>
      <dd>
       &middot; <a target="_blank" href="../charsets/html-charsets.html">HTML 字符集设置</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-ascii.html">HTML ASCII 字符集</a>
      </dd>
     <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6939/">JS 混淆/加密</a>
      </dd> 
      <dd>
       &middot; <a target="_blank" href="https://c.runoob.com/front-end/6232/">PNG/JPEG 图片压缩</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="../tags/html-colorpicker.html">HTML 拾色器</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/53">JSON 格式化工具</a>
      </dd>
      <dd>
       &middot; <a target="_blank" href="..//c.runoob.com/front-end/6680/">随机数生成器</a>
      </dd>
     </dl>
     <dl>
      <dt>
       最新更新
      </dt>
                   <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imread.html" title="Matplotlib imread() 方法">Matplotlib imre...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imsave.html" title="Matplotlib imsave() 方法">Matplotlib imsa...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-imshow.html" title="Matplotlib imshow() 方法">Matplotlib imsh...</a>
      </dd>
              <dd>
       &middot;
      <a href="../matplotlib/matplotlib-hist.html" title="Matplotlib 直方图">Matplotlib 直方图</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-func-object.html" title="Python object() 函数">Python object()...</a>
      </dd>
              <dd>
       &middot;
      <a href="../python3/python-ai-draw.html" title="Python AI 绘画">Python AI 绘画</a>
      </dd>
              <dd>
       &middot;
      <a href="../w3cnote/cursor-editor.html" title="神辅助 Cursor 编辑器，加入 GPT-4 让编码更轻松！">神辅助 Cursor ...</a>
      </dd>
             </dl>
     <dl>
      <dt>
       站点信息
      </dt>
      <dd>
       &middot;
       <a target="_blank" href="mailto:admin@runoob.com" rel="external nofollow">意见反馈</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../disclaimer">免责声明</a>
       </dd>
      <dd>
       &middot;
       <a target="_blank" href="../aboutus">关于我们</a>
       </dd>
      <dd>
       &middot;
      <a target="_blank" href="../archives">文章归档</a>
      </dd>
    
     </dl>
    
     <div class="search-share">
      <div class="app-download">
        <div>
         <strong>关注微信</strong>
        </div>
      </div>
      <div class="share">
      <img width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png" />
       </div>
     </div>
     
    </div>
   </div>
   <div class="w-1000 copyright">
     Copyright &copy; 2013-2023    <strong><a href="../" target="_blank">菜鸟教程</a></strong>&nbsp;
    <strong><a href="../" target="_blank">runoob.com</a></strong> All Rights Reserved. 备案号：<a target="_blank" rel="nofollow" href="https://beian.miit.gov.cn/">闽ICP备15012807号-1</a>
   </div>
  </div>
  <div class="fixed-btn">
    <a class="go-top" href="javascript:void(0)" title="返回顶部"> <i class="fa fa-angle-up"></i></a>
    <a class="qrcode"  href="javascript:void(0)" title="关注我们"><i class="fa fa-qrcode"></i></a>
    <a class="writer" style="display:none" href="javascript:void(0)"   title="标记/收藏"><i class="fa fa-star" aria-hidden="true"></i></a>
    <!-- qrcode modal -->
    <div id="bottom-qrcode" class="modal panel-modal hide fade in">
      <h4>微信关注</h4>
      <div class="panel-body"><img alt="微信关注" width="128" height="128" src="/wp-content/themes/runoob/assets/images/qrcode.png"></div> 
    </div>
  </div>

 <div style="display:none;">
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-84264393-2"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'UA-84264393-2');
</script>
<script>
var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?3eec0b7da6548cf07db3bc477ea905ee";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();
</script>

</div>
<script>
window.jsui={
    www: 'https://www.runoob.com',
    uri: 'https://www.runoob.com/wp-content/themes/runoob'
};
</script>

<script src=""></script>
<script src=""></script>

</body>
</html>